package oneArmedBandit.theGame;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class OneArmedBanditActivity extends Activity
{
	private ImageView wheel1, wheel2, wheel3;
	private AnimationDrawable frameAnimation1, frameAnimation2, frameAnimation3;
	private Button startButton, stopWheel1, stopWheel2, stopWheel3;
	private TextView currentWinnings;
	private AutoStop autoStop;
	int winnings;

	public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        wheel1 = (ImageView) findViewById(R.id.imageViewWheel1);
        wheel2 = (ImageView) findViewById(R.id.imageViewWheel2);
        wheel3 = (ImageView) findViewById(R.id.imageViewWheel3);

        frameAnimation1 = (AnimationDrawable) wheel1.getBackground();
        frameAnimation2 = (AnimationDrawable) wheel2.getBackground();
        frameAnimation3 = (AnimationDrawable) wheel3.getBackground();

        startButton = (Button) findViewById(R.id.buttonStartSpin);
        stopWheel1 = (Button) findViewById(R.id.buttonStop1);
        stopWheel2 = (Button) findViewById(R.id.buttonStop2);
        stopWheel3 = (Button) findViewById(R.id.buttonStop3);
        
        winnings = 20;
        currentWinnings = (TextView) findViewById(R.id.textViewWinnings);
        setTextOfCurrentWinnings();
        
        startButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				frameAnimation1.start();
				frameAnimation2.start();
				frameAnimation3.start();
		        autoStop = new AutoStop();
		        autoStop.start();
				
				startButton.setEnabled(false);
			}
        });
        stopWheel1.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				frameAnimation1.stop();
				compareImages();
			}
        	
        });
        stopWheel2.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				frameAnimation2.stop();
				compareImages();
			}
        	
        });
        stopWheel3.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				frameAnimation3.stop();
				compareImages();
			}
        });
    }

	public void compareImages()
	{
		if (!frameAnimation1.isRunning() && !frameAnimation2.isRunning() && !frameAnimation3.isRunning())
		{
			Bitmap current1 = ((BitmapDrawable)frameAnimation1.getCurrent()).getBitmap();
			Bitmap current2 = ((BitmapDrawable)frameAnimation2.getCurrent()).getBitmap();
			Bitmap current3 = ((BitmapDrawable)frameAnimation3.getCurrent()).getBitmap();
			
			if (current1 == current2 && current2 == current3)
				winnings += 100;
			else if (current1 == current2 || current1 == current3 || current2 == current3)
				winnings += 25;
			else
				winnings--;
			
			if (winnings > 0)
				startButton.setEnabled(true);
			setTextOfCurrentWinnings();
		}
	}
	
	private void setTextOfCurrentWinnings()
	{
		currentWinnings.setText(getString(R.string.winnings) + winnings);
	}
	
	class AutoStop extends Thread
	{
		int delay = 5000;
		
		public void run()
		{
			try
			{ Thread.sleep(delay); }
			catch (InterruptedException e)
			{ e.printStackTrace(); }
			handle.sendEmptyMessage(1);
			try
			{ Thread.sleep(2000); }
			catch(InterruptedException e)
			{ e.printStackTrace(); }
			handle.sendEmptyMessage(2);
			try
			{ Thread.sleep(2000); }
			catch (InterruptedException e)
			{ e.printStackTrace(); }
			handle.sendEmptyMessage(3);
		}
	}
	
	Handler handle = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			switch(msg.what)
			{
			case 1: 
				stopWheel1.performClick();
				break;
			case 2: 
				stopWheel2.performClick();
				break;
			case 3:
				stopWheel3.performClick();
				break;
			}
		};
	};
}